<?php
namespace app\common\service;

use think\facade\Db;

class GetGoodLists extends Base {
    static $AUTH_CHECK = false; //默认必须登录
    public function index(){

        // 帅选
        $pagenum = input('pagenum')?:1;
        $limit = input('limit')?:10;
        $goods_where = ['is_on_sale' => 1,'status' => 1];
        $keywords = input('keyword');
        if($keywords!=''){
            $goods_where['goods_name|keyword'] = ['like','%'.$keywords.'%'];
        }
        //哪个专区
        $leixing_id = input('leixing_id')?:0;
        if($leixing_id){
            $goods_where['leixing_id'] = $leixing_id;
        }
        if(input('sid')){
            $goods_where['sid'] = input('sid');
        }
        if(input('cid')){
            $cid = input('cid');
            $child_cids = Db::name('goods_category')->where('parent_id',$cid)->column('id');
            $child_cids[] = $cid;
            $goods_where['cat_id'] = ['in',$child_cids];

        }
        $order_by_str = 'sort desc,goods_id desc';
        $order_by = input('field');
        if($order_by=='sort'){
            //综合
            $order_by_str = 'rand() desc';
        }elseif($order_by=='sales'){
            //销量
            $order_by_str = 'sales_sum desc';
        }elseif($order_by=='sell_price'){
            $order_str = input('order');
            //价格
            $order_by_str = 'shop_price '.$order_str;
        }elseif($order_by==4){
            //上新
            $order_by_str = 'goods_id desc';
        }

        $data = [];
        $data['map'] = $goods_where;
        $count = Db::name('goods')->where($data['map'])->count();
        //猜您喜欢

        $data['firstRow'] = ($pagenum-1)*$limit+1;
        $data['listRows'] = $limit;
        $data['field'] = 'goods_id,shop_price,level_limit,click_count,market_price,goods_name,goods_remark,leixing_id,score,zs_score,original_img';
        $data['order'] = $order_by_str;

        $goods_list = logic('GoodsLogic')->getListNew($data);
        if($goods_list){
            $domain = getCurUrl();
            foreach($goods_list as $k=>$v){
                $v['image'] = goods_thum_images($v['goods_id'],400,400);
                $v['image'] = $domain.$v['image'];
                $v['original_img'] = $v['original_img'];
                $goods_list[$k] = $v;
            }
        }
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $goods_list,
        ];
        return $result;
    }

    public function basic_info(){
        //商品分类
        $clist = Db::name('goods_category')->where(['is_show'=>1,'parent_id'=>0])->select();
        $result = [
            'status' => true,
            'msg' => '',
            'data' => [
                'clist' => $clist
            ],
        ];
        return $result;
    }

    public function prolist(){
        $tjwhere = [];
        $tjwhere['is_show'] = 1;
        //分类
        if(input('cid')){
            $tjwhere['parent_id'] = input('cid');
            $clist = Db::name('goods_category')->where($tjwhere)->order('sort desc,id')->select();
            if(!$clist) $clist = [];
        }else{
            $clist = Db::name('goods_category')->where($tjwhere)->order('sort desc,id')->select();
            if(!$clist) $clist = [];
        }
        $clist2 = [];
        $rsset=[
            'image_search' => 1
        ];
        $topshowcategory = 0;

        $rdata = [];
        $rdata['status'] = true;
        $rdata['clist'] = $clist;
        $rdata['glist'] = [];
        $rdata['clist2'] = $clist2;
        $rdata['set'] = $rsset;
        $rdata['topshowcategory'] = $topshowcategory;

        return $rdata;
    }


}
